package com.HRs.UI;

import com.HRs.Controller.PatientController;
import com.HRs.domain.Patient;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Register {
    JFrame frame = new JFrame("注册界面");
    JLabel TitleLbl;
    JPanel ImagePanel;
    JPanel p1, p2, p3;
    public Register(){
        frame.setLayout(new GridLayout(5, 1));

        //----------------文字标题标签------------------
        TitleLbl = new JLabel();
        TitleLbl.setText("注  册");
        TitleLbl.setFont(new Font("隶书", Font.BOLD, 75));
        TitleLbl.setHorizontalAlignment(SwingConstants.CENTER);
        frame.add(BorderLayout.NORTH, TitleLbl);



//        ----------------主体----------------------
        JLabel namelbl = new JLabel("姓名:");
        namelbl.setFont(new Font("隶书", Font.PLAIN, 25));
        JTextField txtname = new JTextField(20);
        JLabel phoneNUm = new JLabel("     电话:");
        phoneNUm.setFont(new Font("隶书", Font.PLAIN, 25));
        JTextField txtxp = new JTextField(20);
        p1 = new JPanel();
        p1.setLayout(new FlowLayout());
        p1.add(namelbl);
        p1.add(txtname);
        p1.add(phoneNUm);
        p1.add(txtxp);
        frame.add(p1);

        JLabel sexlbl = new JLabel("性别:");
        sexlbl.setFont(new Font("隶书", Font.PLAIN, 25));
        JComboBox<String> box = new JComboBox<>();
        box.addItem("---请选择性别---");
        box.addItem("男");
        box.addItem("女");
        box.setFont(new Font("隶书", Font.PLAIN, 15));
        JLabel addlbl = new JLabel("     住址:");
        addlbl.setFont(new Font("隶书", Font.PLAIN, 25));
        JTextField txtadd = new JTextField(30);
        p2 = new JPanel();
        p2.setLayout(new FlowLayout());
        p2.add(sexlbl);
        p2.add(box);
        p2.add(addlbl);
        p2.add(txtadd);
        frame.add(p2);

        JLabel idlbl = new JLabel("     密码:");
        idlbl.setFont(new Font("隶书", Font.PLAIN, 25));
        JTextField txtid = new JTextField(20);
        JLabel passwordlbl = new JLabel("身份证号:");
        passwordlbl.setFont(new Font("隶书", Font.PLAIN, 25));
        JTextField txtpassword = new JTextField(20);
        p3 = new JPanel();
        p3.setLayout(new FlowLayout());
        p3.add(passwordlbl);
        p3.add(txtpassword);
        p3.add(idlbl);
        p3.add(txtid);
        frame.add(p3);

        JButton b2 = new JButton("确认注册");
        b2.setFont(new Font("隶书", Font.ITALIC, 30));
        b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                PatientController pc = new PatientController();
                 String p_Pid = pc.get_Pid();
                 String p_Name = txtname.getText();
                 String p_Password = txtpassword.getText();
                 String p_Sex = (String) box.getSelectedItem();
                 String p_Address = txtadd.getText();
                 String p_ID = txtid.getText();
                 String P_PhoneNumber = txtxp.getText();
                Patient patient = new Patient(p_Name, p_ID, p_Sex, p_Address, p_Password, P_PhoneNumber);
                System.out.println(patient);
                PatientController PC = new PatientController();
                if(PC.RegisterFor_p(patient)){
                    JOptionPane.showMessageDialog(b2, "注册成功！您的账号为:" + p_Pid, "提示消息", JOptionPane.PLAIN_MESSAGE);
                    new Login();
                    frame.dispose();
                }else{
                    JOptionPane.showMessageDialog(b2, "注册失败！", "提示消息", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
//        b2.setSize(75,150);
        JPanel p5 = new JPanel();
        p5.add(b2);
        frame.add(p5);
        //--------------背景图片设置---------------
        ImageIcon background = new ImageIcon("JDBC\\images\\b173fcf4f0b25c4cd00f73ab665440e5.jpeg");//背景图片
        //设置背景图片的大小与窗体一致
        background.setImage(background.getImage().getScaledInstance(1000, 800, Image.SCALE_DEFAULT));

        JLabel label1 = new JLabel(background);//把背景图片显示在一个标签里

        label1.setSize(1000, 800);
        ImagePanel = (JPanel) frame.getContentPane();
        ImagePanel.setOpaque(false);
        frame.getLayeredPane().setLayout(null);
        frame.getLayeredPane().add(label1, Integer.valueOf((Integer.MIN_VALUE)));

        p1.setOpaque(false);
        p2.setOpaque(false);
        p3.setOpaque(false);
        p5.setOpaque(false);


        frame.setSize(800, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new Register();
    }
}
